package com.readaloud.junit;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.junit.Before;
import org.junit.Test;

import com.readaloud.dao.InvertedIndexDao;
import com.readaloud.dao.InvertedIndexDaoImpl;
import com.readaloud.hibernate.user.Author;
import com.readaloud.hibernate.user.Books;
import com.readaloud.hibernate.user.Users;

public class InvertedIndexDaoTest {
	private InvertedIndexDao invertedIndexDao;
	private String password;
	private String userName;
	private String userType;
	private String userTypeName;
	private int userId;
	private int authorId;
	private String authorType;
	private String authorName;
	private String authorTypeName;
	private String authorPassword;

	@Before
	public void init() {
		invertedIndexDao = new InvertedIndexDaoImpl();
		userId = 1;
		password = "Scott";
		userName = "Tiger";
		userType = "reader";
		authorType = "author";
		authorName = "AuthorName";
		authorTypeName = "AuthorTypeName";
		authorPassword = "AuthorPassword";
	}

	public void testInitialize() {
		// File dir = new File("D://CMPE226//books//");
		File dir = new File("D://CMPE226//books//11.txt");
		invertedIndexDao.intialize(dir);

	}

	@Test
	public void testQuery() {
		Books book = new Books();
		book = setBook(book);
		List<Books> expectedBookObject = new ArrayList<Books>();
		expectedBookObject.add(book);
		String query = "kitten";
		InvertedIndexDao invertedIndexDaoImpl = new InvertedIndexDaoImpl();
		List<Books> books = invertedIndexDaoImpl.query(query);
		// books.equals(expectedBookObject);
	}

	@Test
	public void testInitializeAndQuery() {
		testInitialize();
		testQuery();
	}

	private Books setBook(Books book) {
		book.setAuthor("Charles Dodgson, AKA Lewis Carroll");
		return book;
	}

	@Test
	public void testSaveUser() {

		saveUser();
		SessionFactory sessionFactory = new Configuration().configure()
				.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Users u where u.userName='"
				+ getUserName() + "'");
		List<Users> list = q.list();
		Users user = list.get(0);
		setUserId(user.getUserId());
		session.close();
		Assert.assertEquals(getUserName(), user.getUserName());
		Assert.assertEquals(getPassword(), user.getPassword());
		Assert.assertEquals(getUserType(), user.getUserType());
	}

	private void saveUser() {
		ArrayList<String> paramList = new ArrayList<String>();
		paramList.add(getUserName());
		paramList.add(getPassword());
		paramList.add(getUserTypeName());
		paramList.add(getUserType());
		invertedIndexDao.saveUser(paramList);
	}

	private void saveAuthor() {
		ArrayList<String> paramList = new ArrayList<String>();
		paramList.add(getAuthorName());
		paramList.add(getAuthorPassword());
		paramList.add(getAuthorTypeName());
		paramList.add(getAuthorType());
		invertedIndexDao.saveUser(paramList);
	}
	
	@Test
	public void testIsValidUser() {
		saveUser();

		Users user = invertedIndexDao.isValidUser(getUserName(), getPassword());
		Assert.assertNotNull("User is invalid", user);
	}

	@Test
	public void testUpdateBiography() {
		saveAuthor();
		SessionFactory sessionFactory = new Configuration().configure()
				.buildSessionFactory();
		Session session = sessionFactory.openSession();
		session.beginTransaction();
		Query q = session.createQuery("from Author a where a.userName='"
				+ getUserName() + "'");
		List<Users> list = q.list();
		Users user = list.get(0);
		invertedIndexDao.updateBiography(user.getUserId(), "testing Biography!");
		Author actualAuthor = invertedIndexDao.getAuthorDetails(user.getUserId());
		Assert.assertEquals("testing Biography!", actualAuthor.getBiography());
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public String getUserTypeName() {
		return userTypeName;
	}

	public void setUserTypeName(String userTypeName) {
		this.userTypeName = userTypeName;
	}

	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	public int getAuthorId() {
		return authorId;
	}

	public void setAuthorId(int authorId) {
		this.authorId = authorId;
	}

	public String getAuthorType() {
		return authorType;
	}

	public void setAuthorType(String authorType) {
		this.authorType = authorType;
	}

	public String getAuthorName() {
		return authorName;
	}

	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}

	public String getAuthorTypeName() {
		return authorTypeName;
	}

	public void setAuthorTypeName(String authorTypeName) {
		this.authorTypeName = authorTypeName;
	}

	public String getAuthorPassword() {
		return authorPassword;
	}

	public void setAuthorPassword(String authorPassword) {
		this.authorPassword = authorPassword;
	}
}
